package org.example.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @description: 消息生产者
 */
public class TopicProducerTest {

    //交换机名称
    public static final String EXCHANGE_TOPIC = "EXCHANGE_TOPIC";

    /**
     * 步骤：
     * 1、创建连接
     * 2、创建信道
     * 3、创建交换机
     * 4、发送消息
     */
    public static void main(String[] args) throws Exception {
        //定义连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //设置连接的mq服务器的IP地址
        factory.setHost("localhost");
        //1、创建连接
        try(Connection connection = factory.newConnection()){
            //2、创建信道
            Channel channel = connection.createChannel();
            //3、创建交换机
            channel.exchangeDeclare(EXCHANGE_TOPIC, BuiltinExchangeType.TOPIC);

            //4、发送消息
//            for (int i = 1; i <= 5; i++) {
                int i = 0;
                channel.basicPublish(EXCHANGE_TOPIC, "info", null, ("info" + i).getBytes());
                channel.basicPublish(EXCHANGE_TOPIC, "pethome.userservice.error", null, ("pethome.userservice.error" + i).getBytes());
                channel.basicPublish(EXCHANGE_TOPIC, "pethome.userservice.error.info", null, ("pethome.userservice.error.info" + i).getBytes());
                channel.basicPublish(EXCHANGE_TOPIC, "pethome.info", null, ("pethome.info" + i).getBytes());
//            }
            System.out.println("消息发送完毕");
        }
    }
}